﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 公司
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TCompanyController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public TCompanyController(ITCompanyRepository tcomService, ILogRepository logRepository)
        {
            TcomService = tcomService;
            this.logRepository = logRepository;
        }

        public ITCompanyRepository TcomService { get; }
        /// <summary>
        /// 添加公司信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTcompany(TCompany obj)
        {
            ApiResult apiresult = new ApiResult();
            bool IsOK = TcomService.AddTcompany(obj);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了公司" + obj.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "添加成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "添加失败!编码已存在！";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 显示公司信息
        /// </summary>
        /// <param name="hname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTcompanyList(string? hname = "", int pageIndex = 1, int pageSize = 3)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            ApiResult apiresult = new ApiResult();
            int totalCount;
            var list = TcomService.GetTcompanyList(hname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = list;
                apiresult.count = totalCount;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 删除公司信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTcompany(int uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiresult = new ApiResult();
            bool IsOK = TcomService.DelTcompany(uid);
            if (IsOK)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了公司";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }

        /// <summary>
        /// 批量删除公司信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTcompanyList(int[] uidList)
        {
            if (uidList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            ApiResult apiresult = new ApiResult();
            foreach (int uid in uidList) 
            {
                bool IsOK = TcomService.DelTcompany(uid);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = IsOK ? uname + "删除了公司" : uname + "删除公司失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!IsOK)
                {
                    daRes = false;
                    break;
                }
            }
            
            if (daRes)
            {
                apiresult.code = "200";
                apiresult.message = "删除成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "删除失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 修改公司信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateTcompany(TCompany obj)
        {
            if (obj.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(obj.Code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            var res = TcomService.UpdateTcompany(obj);
            ApiResult apiresult = new ApiResult();
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了公司" + obj.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiresult.code = "200";
                apiresult.message = "修改成功!";
                apiresult.data = true;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "修改失败!编码已存在！";
            apiresult.data = true;
            return Ok(apiresult);
        }
        /// <summary>
        /// 根据id获取公司信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTcompanyById(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = TcomService.GetTcompanyById(id);
            ApiResult apiresult = new ApiResult();
            if (res != null)
            {
                apiresult.code = "200";
                apiresult.message = "获取成功!";
                apiresult.data = res;
                return Ok(apiresult);
            }
            apiresult.code = "500";
            apiresult.message = "获取失败!";
            apiresult.data = true;
            return Ok(apiresult);
        }
    }
}
